﻿using System;
using System.Collections.Generic;

using VirtualTournaments.DataAccess;
using VirtualTournaments.DataAccess.EntityManagers;

namespace VirtualTournaments.Services.Users
{
	public class UsersService
	{
		public static User getUser(String userName)
		{
			List<User> result = new List<User>();
			IUsersManager usersManager = DataAccessManager.EntityManagersFactory.GetUsersManager();
			// TODO: call UserManager.getUser.ToList().ForEach(t => result.Add(BusinessObject2ServiceObject(t)));

			

            return new User()
            {
                name = "raul",
                password = "raul",
                resgistration = new List<Services.Tournaments.Tournament>()
                
            };


        }
        private static User BusinessObject2ServiceObject(Business.Users.User businessObject)
        {
            return new User()
            {
                // TODO: extraer lo que necesito del objeto BusinessObject
            };
        }

        public static void registerUserInTournament(Services.Users.User user, String tournamentName)
        {
            Services.Tournaments.Tournament tournament = Services.Tournaments.TournamentsService.GetTournamentByName(tournamentName);
            user.resgistration.Add(tournament);

        }

        public static Services.Users.UserRegistrationType[] getUserActiveTournamentRegistration(Services.Users.User user)
        {
            List<UserRegistrationType> userActiveTournamentRegistration = new List<UserRegistrationType>();
            bool isRegistered;

            Services.Tournaments.Tournament[] activeTournamentsCollection = Services.Tournaments.TournamentsService.GetActiveTournaments();

            foreach (var activeTournament in activeTournamentsCollection)
            {
                isRegistered = false;

                foreach (var userRegisteredTournament in user.resgistration)
                {
                    if (activeTournament.Name == userRegisteredTournament.Name)
                    {
                        isRegistered = true;
                        break;
                    }
                }
                userActiveTournamentRegistration.Add(new UserRegistrationType(activeTournament, isRegistered));

            }
            return userActiveTournamentRegistration.ToArray();
        }
        
        }
}
